package cn.maihe.elg.operation.centers.epoint.handler.service;

import cn.maihe.elg.operation.centers.epoint.config.EpointCenterInfosConfig;
import cn.maihe.elg.operation.centers.epoint.dto.EpointBaseRespDTO;
import cn.maihe.elg.operation.centers.epoint.dto.handler.EpointRevokeApplyGuaranteeReq;
import cn.maihe.elg.operation.centers.epoint.handler.EpointCenterTradeService;
import cn.maihe.elg.operation.centers.supports.CenterTradeContext;
import cn.maihe.elg.operation.centers.supports.annotation.CenterTradeService;
import cn.maihe.elg.operation.model.enums.AcceptOrderStatus;
import cn.maihe.elg.operation.model.enums.CenterNoEnum;
import cn.maihe.elg.operation.model.enums.CenterProviderEnum;
import cn.maihe.elg.operation.model.enums.TradeType;
import cn.maihe.elg.operation.repository.entity.AcceptOrderInfoDO;
import cn.maihe.elg.operation.repository.entity.ProjectInfoDO;
import cn.maihe.elg.operation.repository.entity.RevokeApplayOrderInfoDO;
import cn.maihe.elg.operation.service.business.ProjectInfoService;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import cn.maihe.elg.operation.utils.IdGenerate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * @Description 放弃申请通知接口 ： 若未确认支付则允许放弃申请，若已确认支付者不允许发起申请
 * @Author Alan
 * @Date 2025/05/30.
 */
@CenterTradeService(
        provider = CenterProviderEnum.EPOINT,
        centerNo = CenterNoEnum.EPOINT_NINGDE,
        tradeType = TradeType.giveupnotice,
        reqClass = EpointRevokeApplyGuaranteeReq.class,
        respClass = EpointBaseRespDTO.class
)
@Slf4j
public class NDGiveUpNoticeService extends EpointCenterTradeService<EpointRevokeApplyGuaranteeReq, EpointBaseRespDTO> {

    public NDGiveUpNoticeService(EpointCenterInfosConfig centerInfosConfig) {
        super(centerInfosConfig);
    }

    @Resource
    private AcceptOrderInfoService acceptOrderInfoService;
    @Resource
    private ProjectInfoService projectInfoService;

    @Override
    protected void verifyParam(CenterTradeContext<EpointRevokeApplyGuaranteeReq, EpointBaseRespDTO> context) {
        super.verifyParam(context);
        EpointRevokeApplyGuaranteeReq req = context.getReq();
        String orderNo = req.getApplyno();
        Assert.notNull(orderNo, () -> "业务流水号不能为空");
    }

    /**
     * 具体业务处理
     *
     * @param context
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public EpointBaseRespDTO processService(CenterTradeContext<EpointRevokeApplyGuaranteeReq, EpointBaseRespDTO> context) {

        EpointRevokeApplyGuaranteeReq req = context.getReq();
        Long centerId = context.getCenterId();
        Long orgId = context.getOrgId();

        AcceptOrderInfoDO orderInfoDO = acceptOrderInfoService.getByCenterOrderId(centerId, orgId, req.getApplyno());
        if (orderInfoDO == null) {
            return EpointBaseRespDTO.error("订单信息不存在");
        }

        if (orderInfoDO.getStatus() == AcceptOrderStatus.HAS_OUT_GUARANTEE.getStatus()) {
            return EpointBaseRespDTO.error("已出函，不能放弃申请");
        }
        if (orderInfoDO.getStatus() == AcceptOrderStatus.FINISH.getStatus()) {
            return EpointBaseRespDTO.error("保函已完成，不能放弃申请");
        }

        AcceptOrderInfoDO updateOrderInfo = AcceptOrderInfoDO.builder().id(orderInfoDO.getId())
                .status(AcceptOrderStatus.CANCEL.getStatus()).remark("放弃申请关闭订单").build();

        ProjectInfoDO project = projectInfoService.selectById(orderInfoDO.getProjectId());

        RevokeApplayOrderInfoDO revokeApplayOrderInfoDO = RevokeApplayOrderInfoDO.builder()
                .id(IdGenerate.getId())
                .centerId(centerId)
                .orgId(orgId)
                .centerOrderId(req.getApplyno())
                .revokeNo(req.getApplyno())
                .projectCode(project.getProjectCode()).projectName(orderInfoDO.getProjectName())
                .sectionCode(project.getSectionCode()).sectionName(orderInfoDO.getSectionName())
                .tenderer(project.getTenderer()).status(1)
                .build();

        acceptOrderInfoService.revokeOrder(updateOrderInfo, revokeApplayOrderInfoDO);


        return EpointBaseRespDTO.success("放弃投保申请已完成");
    }
}
